import { StepSoundParams as Params } from '@swda/configs/params'

let SWDA_StepSound = {};



//=============================================================================
// Game_CharacterBase
//=============================================================================
SWDA_StepSound.Game_Player_updatePattern = Game_Player.prototype.updatePattern;
Game_Player.prototype.updatePattern = function () {
    SWDA_StepSound.Game_Player_updatePattern.call(this);
    var stepSoundPan = (this.screenX() - Graphics.boxWidth / 2) / Graphics.boxWidth * 200;
    //var soundStep = ((this._pattern + 1) % (2 * (2 - this.isDashing())) == 0);
    var soundStep = ((this._pattern + 1) % (Math.floor(this._maxFrame / 2)) == 0);
    if (this.isMoving() && soundStep) {
        SoundManager.playStepSound(stepSoundPan, true);
    }
};

/*跟随玩家音效，备用
SWDA_StepSound.Game_Follower_updatePattern = Game_Follower.prototype.updatePattern;
Game_Follower.prototype.updatePattern = function() {
    SWDA_StepSound.Game_Follower_updatePattern.call(this);
    var stepSoundPan = (this.screenX()-Graphics.boxWidth/2)/Graphics.boxWidth*200;
    //var soundStep = ((this._pattern + 1) % (2 * (2 - this.isDashing())) == 0);
    var soundStep = ((this._pattern + 1) % (Math.floor(this._maxFrame/2)) == 0);
    if (this.isMoving() && soundStep){
        SoundManager.playStepSound(stepSoundPan);
    }
};
*/

//=============================================================================
// SoundManager
//=============================================================================

SoundManager.playStepSound = function (stepSoundPan, isPlayer) {
    var isPlayer = isPlayer || false;
    if (isPlayer)
        AudioManager.playSe($gameSystem.getStepSound(stepSoundPan, true));
    else//跟随玩家音效不同
        AudioManager.playSe($gameSystem.getStepSound(stepSoundPan, false));
};

//=============================================================================
// Game_System
//=============================================================================


Game_System.prototype.initStepSounds = function () {
    this._stepSoundEnable = Params.StepSoundEnable;
    this._stepSoundName = Params.StepSoundName;
    this._stepSoundVol = Params.StepSoundVolume;
    this._stepSoundVolVar = Params.StepVolumeVar;
    this._stepSoundPitch = Params.StepSoundPitch;
    this._stepSoundPitchVar = Params.StepPitchVar;
};


Game_System.prototype.getStepSound = function (stepSoundPan, isPlayer) {
    isPlayer = isPlayer || false;
    if (this._stepSoundName === undefined) this.initStepSounds();
    let obj = {
        name: this._stepSoundName,
        volume: this._stepSoundVol,
        pitch: this._stepSoundPitch,
        pan: stepSoundPan
    }

    let max = this._stepSoundVol + this._stepSoundVolVar;
    let min = this._stepSoundVol - this._stepSoundVolVar;

    if (isPlayer) {
        obj['volume'] = Math.floor(Math.random() * (max - min + 1) + min);
    } else {
        obj['volume'] = Math.floor(Math.random() * (max - min + 1) + min) * 0.2;
    }

    max = this._stepSoundPitch + this._stepSoundPitchVar;
    min = this._stepSoundPitch - this._stepSoundPitchVar;

    obj['pitch'] = Math.floor(Math.random() * (max - min + 1) + min);
    return obj;
};